<?php
/**
 * Created by PhpStorm.
 * User: lizi
 * Date: 2021/09/03
 * Time: 17:53
 */

namespace App\Console\Commands;

use App\Models\MemberAlipayRisk;
use App\Models\MemberAlipayRiskCredit;
use App\Models\MemberDlzwRisk;
use App\Models\MemberQnRisk;
use App\Models\OrderDlzwRisk;
use App\Models\OrderGoods;
use App\Models\OrderLease;
use App\Models\Suggest;
use App\Models\SuggestLog;
use App\Models\Order;
use App\Models\MemberOther;
use App\Services\RadarService;
use DateTime;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use PDO;

class RiskOrderDataSyncCommand extends Command
{
    /**
     * The name and signature of the console command.
     * @var string
     */
    protected $signature = 'RiskOrderDataSync';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '订单数据同步到risk数据库';

    /**
     * Create a new command instance.
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

        set_time_limit(0);
        ini_set('memory_limit', '-1');


        Order::query()->where('status', '>', 0)
            ->where('create_time', '>',strtotime(date('2024-10-01')))//strtotime(date('Y-m-d', strtotime('-1 day')))
            ->where('is_lock', 0)
            ->chunk(300, function ($orders) {
                // 准备要插入或更新的数据
                $dataToUpsert =obToArray($orders);

                $qn=[];
                $dlz=[];
                $alipayRisk=[];
                $riskCredit=[];
                $orderIdsOfLease=[];
                foreach ($dataToUpsert as $key => &$value){
                    if($value['status']>4&&$value['status']!=16){
                        $orderIdsOfLease[]=$value['id'];
                    }

                    if($value['status']=='38'){
                        $value['status']='39';
                    }

                    $MemberDlzwRisk= OrderDlzwRisk::query()->where('order_id',$value['id'])->first();
                    if($MemberDlzwRisk){
                        $MemberDlzwRisk['create_time']=date('Y-m-d H:i:s',$value['create_time']);
                        $dlz[]=$MemberDlzwRisk;
                    }


                }


                $dlz=obToArray($dlz);
                if (!empty($dlz)) {
                    DB::connection('mysql_copy_risk')->transaction(function () use ($dlz) {
                        DB::connection('mysql_copy_risk')->table('sp_chaoshengxin_member_dlzw_risk')->upsert(
                            $dlz,
                            ['id']
                        );
                    });
                    $this->info('处理了 ' . count($MemberDlzwRisk) . '条 MemberDlzwRisk 记录');
                }



                // 使用事务包裹 upsert 操作
                DB::connection('mysql_copy_risk')->transaction(function () use ($dataToUpsert) {
                    DB::connection('mysql_copy_risk')->table('sp_chaoshengxin_order_2')->upsert(
                        $dataToUpsert,
                        ['id']
                    );
                });

                $this->info('处理了 ' . count($dataToUpsert) . ' 条订单记录');

                $orderIds = array_column($dataToUpsert, 'id');



                // 处理 OrderLease 数据
                $OrderLease = OrderLease::whereIn('order_id', $orderIdsOfLease)->get()->toArray();
                if (!empty($OrderLease)) {
                    foreach ($OrderLease as $key => &$value){
                        $value['begin_time']=date('Y-m-d H:i:s',$value['begin_time']);
                        $value['end_time']=date('Y-m-d H:i:s',$value['end_time']);
                        if($value['create_time']){
                            $value['create_time']=date('Y-m-d H:i:s',$value['create_time']);
                        }else{
                            $value['create_time'] =null;
                        }
                        if($value['pay_time']) {
                            $value['pay_time'] = date('Y-m-d H:i:s', $value['pay_time']);
                        }else{
                            $value['pay_time'] =null;
                        }
                    }

                    DB::connection('mysql_copy_risk')->transaction(function () use ($OrderLease) {
                        DB::connection('mysql_copy_risk')->table('sp_chaoshengxin_order_lease_2')->upsert(
                            $OrderLease,
                            ['id']
                        );
                    });
                    $this->info('处理了 ' . count($OrderLease) . ' 条 OrderLease 记录');
                }


                // 处理 OrderGoods 数据
                $OrderGoods = OrderGoods::whereIn('order_id', $orderIds)->get()->toArray();
                if (!empty($OrderGoods)) {
                    DB::connection('mysql_copy_risk')->transaction(function () use ($OrderGoods) {
                        DB::connection('mysql_copy_risk')->table('sp_chaoshengxin_order_goods_2')->upsert(
                            $OrderGoods,
                            ['id']
                        );
                    });
                    $this->info('处理了 ' . count($OrderGoods) . ' 条 OrderGoods 记录');
                }

                // 获取用户 ID
                $userIds = array_column($dataToUpsert, 'uid');

                // 处理 MemberOther 数据
                $MemberOther = MemberOther::whereIn('id', $userIds)->get()->toArray();
                if (!empty($MemberOther)) {
                    DB::connection('mysql_copy_risk')->transaction(function () use ($MemberOther) {
                        DB::connection('mysql_copy_risk')->table('sp_chaoshengxin_member_2')->upsert(
                            $MemberOther,
                            ['id']
                        );
                    });
                    $this->info('处理了 ' . count($MemberOther) . ' 条 MemberOther 记录');
                }



            });

    }
}
